//
//  EidtInfoViewController.m
//  Yearly Plan
//
//  Created by admin on 2019/2/27.
//  Copyright © 2019年 admin. All rights reserved.
//

#import "EidtInfoViewController.h"


#import "HcdDateTimePickerView.h"
@interface EidtInfoViewController ()<UITextFieldDelegate>
{
    HcdDateTimePickerView * dateTimePickerView;
}
@property (weak, nonatomic) IBOutlet UITextView *infoTextView;
@property (weak, nonatomic) IBOutlet UITextField *nameTF;
@property (weak, nonatomic) IBOutlet UITextField *timeTF;

@end

@implementation EidtInfoViewController

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    return NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"Eidt";
    self.timeTF.delegate = self;
    
    self.view.backgroundColor = HEX_COLOR(0xF1F1F1);
    
    UIBarButtonItem *leftBarItem = [[UIBarButtonItem alloc] initWithTitle:@"cancel" style:UIBarButtonItemStylePlain target:self action:@selector(cancelBtnClick)];
    self.navigationItem.leftBarButtonItem = leftBarItem;
    
    UIBarButtonItem *rightBarItem = [[UIBarButtonItem alloc] initWithTitle:@"save" style:UIBarButtonItemStylePlain target:self action:@selector(saveBtnClick)];
    self.navigationItem.rightBarButtonItem = rightBarItem;
    
    self.nameTF.layer.cornerRadius = 5.0;
    [self.nameTF setValue:HEX_COLOR(0x9A9A9A) forKeyPath:@"_placeholderLabel.textColor"];
    [self.nameTF setValue:[UIFont systemFontOfSize:14] forKeyPath:@"_placeholderLabel.font"];
    self.timeTF.layer.cornerRadius = 5.0;
    [self.timeTF setValue:HEX_COLOR(0x9A9A9A) forKeyPath:@"_placeholderLabel.textColor"];
    [self.timeTF setValue:[UIFont systemFontOfSize:14] forKeyPath:@"_placeholderLabel.font"];
    self.infoTextView.layer.cornerRadius = 5.0;
    self.infoTextView.placeholder = @" Click here and enter event content";
    self.infoTextView.placeholderColor = HEX_COLOR(0x9A9A9A);
    
    if (self.name.length!=0) {
        self.nameTF.text = self.name;
    }
    if (self.time.length!=0) {
        self.timeTF.text = self.time;
    }
    if (self.content.length!=0) {
        self.infoTextView.placeholder = @"";
        self.infoTextView.text = self.content;
    }
}

-(void)cancelBtnClick {
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)saveBtnClick {
    if (self.nameTF.text.length==0) {
        
        [self showHint:@"Please enter the event name"];
        return;
    }
    if (self.timeTF.text.length==0) {
        [self showHint:@"Please enter the event time"];
        return;
    }
    if (self.infoTextView.text.length==0) {
        [self showHint:@"Please enter the content"];
        return;
    }
    else {
        NSString *nameStr = [self.nameTF.text copy];
        NSString *timeStr = [self.timeTF.text copy];
        NSString *contentStr = [self.infoTextView.text copy];
        NSMutableDictionary *dataDic = [NSMutableDictionary dictionary];
        [dataDic setValue:nameStr forKey:@"name"];
        [dataDic setValue:timeStr forKey:@"time"];
        [dataDic setObject:contentStr forKey:@"content"];
        
        NSMutableArray *array = (NSMutableArray *)[Defaults objectForKey:weiwancheng];
        NSMutableArray *dataArray = [NSMutableArray arrayWithArray:array];
        [dataArray addObject:dataDic];
        [Defaults setObject:dataArray forKey:weiwancheng];
        
        
        [self showHint:@"Save successfully"];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1. * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.navigationController popViewControllerAnimated:YES];
        });
        
        NSLog(@"存储：%@", dataArray);
    }
    
}
- (IBAction)chooseTimeBtnClick:(id)sender {
    
    [self showTimePicker];
}

-(void)showTimePicker {
    __block EidtInfoViewController *weakSelf = self;
    dateTimePickerView = [[HcdDateTimePickerView alloc] initWithDatePickerMode:DatePickerDateTimeMode defaultDateTime:[[NSDate alloc]initWithTimeIntervalSinceNow:1000]];
    dateTimePickerView.topViewColor = HEX_COLOR(0xF7CE47);
    dateTimePickerView.buttonTitleColor = [UIColor blackColor];
    [dateTimePickerView setMinYear:2019];
    [dateTimePickerView setMaxYear:2050];
    dateTimePickerView.clickedOkBtn = ^(NSString * datetimeStr){
        NSLog(@"%@", datetimeStr);
        weakSelf.timeTF.text = datetimeStr;
    };
    if (dateTimePickerView) {
        [self.view addSubview:dateTimePickerView];
        [dateTimePickerView showHcdDateTimePicker];
    }
}

@end
